<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  通话评价
 */
class CallComment extends Model
{
    protected $table = 'call_comment';

    const LEVEL = [
        1 => '满意',
        2 => '一般',
        3 => '不满意'
    ];

    const TAG = [
        1 => ['tag_id' => 1, 'text' => '有话题'],
        ['tag_id' => 2, 'text' => '善解人意'],
        ['tag_id' => 3, 'text' => '整体不错'],
        ['tag_id' => 4, 'text' => '活泼热情'],
        ['tag_id' => 5, 'text' => '人美声甜'],
    ];

    static function addRecord(
        $userId = 0,
        $callRecord = null,
        $level = 1,
        $tag = '',
        $desc = ''
    ) {
        $model = new self();
        $model->user_id = $userId;
        $model->call_id = $callRecord->id;
        $model->call_type = $callRecord->call_type;
        $model->channel_id = $callRecord->channel_id;
        $model->level = $level;
        $model->tag = $tag;
        $model->desc = $desc;
        $model->saveOrFail();
    }

    static function getDetailByChannel($channelId = '')
    {
        $item = self::where('channel_id', $channelId)->first();
        if (!$item) return [];
        $tags = explode(',', $item->tag);
        if (!$item->tag || !$tags) $tags = [];
        else {
            foreach ($tags as &$tag) $tag = [
                'tag_id' => $tag,
                'text' => self::TAG[$tag]['text']
            ];
        }
        return [
            'level' => $item->level,
            'desc' => $item->desc,
            'tags' => $tags
        ];
    }

    static function getCommentsByChannel($channelIds = [])
    {
        $items = self::whereIn('channel_id', $channelIds)->get()->toArray();
        $comments = [];
        foreach ($items as $item) $comments[$item['channel_id']][] = self::LEVEL[$item['level']];
        return $comments;
    }
}
